package cn.chinasyq.photoquan.photo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.photo.adapter.PhotoCommentAdapter;
import cn.chinasyq.photoquan.photo.bean.CommentsEntity;
import cn.chinasyq.photoquan.photo.mode.PhotoMode;
import cn.chinasyq.photoquan.user.activity.LoginActivity;
import cn.chinasyq.photoquan.view.LoadView;
import cn.master.util.utils.RequestCodeUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.xlistview.view.XListView;

/**
 * Created by 一搏 on 2015/11/25.
 */

public class PhotoCommentActivity extends Activity implements View.OnClickListener, ResponseHandler.OnNeedLoginListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnFailedListener, TextView.OnEditorActionListener {
    public static final String UUID = "uuid";
    public static final String TAG_COMMENT = "comment";
    private static final String TAG_UP = "up";
    public static final String TYPE = "type";
    private String article_uuid;
    private XListView listView;
    private PhotoCommentAdapter adapter;
    private LoadView loadView;
    private TextView tv_title;
    private List<CommentsEntity> info;
    private String type;

    public void callLoginActivity() {
        Intent intent = new Intent(PhotoCommentActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_comment);
        article_uuid = getIntent().getStringExtra(UUID);
        type = getIntent().getStringExtra(TYPE);
        listView = (XListView) findViewById(R.id.xlv_content);
        adapter = new PhotoCommentAdapter();
        listView.setAdapter(adapter);
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(false);
        loadView = (LoadView) findViewById(R.id.loadview);
        loadView.setStatus(LoadView.Status.loading);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("评论");
        loadView.setOnReLoadClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshDate();
            }
        });
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(new XListView.IXListViewListener() {

            @Override
            public void onRefresh() {
                refreshDate();
            }

            @Override
            public void onLoadMore() {
                loadmoreDate();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshDate();
    }

    private static final String TAG_LOADMORE = "loadmore";
    private static final String TAG_REFRESH = "refresh";


    protected void refreshDate() {
        ResponseHandler handler = new ResponseHandler(TAG_REFRESH);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        PhotoMode.obtainPhotoCommentList(this, handler, article_uuid, type, PhotoMode.MODE_REFRESH);
    }

    protected void loadmoreDate() {
        ResponseHandler handler = new ResponseHandler(TAG_LOADMORE);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        PhotoMode.obtainPhotoCommentList(this, handler, article_uuid, type, PhotoMode.MODE_LOAD_MORE);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.ibtn_back:
                onBackPressed();
                break;
            case R.id.tv_like_num:
                temp = (CheckedTextView) v;
                upComment((String) v.getTag());
                break;
            case R.id.et_comment:
                Intent i = new Intent(getIntent());
                i.setClass(PhotoCommentActivity.this, SendCommentActivity.class);
                startActivityForResult(i, RequestCodeUtil.getInstance().obtainRequestCode(SendCommentActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onNeedLogin(String tag) {
        callLoginActivity();
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }

        if (tag.equals(TAG_LOADMORE)) {
            listView.stopLoadMore();
        } else if (tag.equals(TAG_UP)) {

        } else {
            loadView.setStatus(status == ResponseHandler.FailedStatus.DATA_ERROR ? LoadView.Status.data_error : LoadView.Status.network_error);
            listView.stopRefresh();
        }
    }

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        if (tag.equals(TAG_REFRESH)) {
            info = (ArrayList<CommentsEntity>) data;
            adapter.setInfoEntity(info);
            listView.setAdapter(adapter);
            listView.stopRefresh();
            if (info == null) {
                loadView.setStatus(LoadView.Status.not_data);
                return;
            } else {
                loadView.setStatus(LoadView.Status.successed);
            }


        }
        if (tag.equals(TAG_COMMENT)) {
            ((EditText) findViewById(R.id.et_comment)).setText("");
            refreshDate();
        }
        if (tag.equals(TAG_UP)) {
            temp.setChecked(true);
            temp.setEnabled(false);
            temp.setText(String.valueOf(Integer.parseInt(temp.getText().toString()) + 1));
        }
        if (tag.equals(TAG_LOADMORE)) {
            adapter.addInfoEntity((List<CommentsEntity>) data);
            listView.stopLoadMore();
        }

    }

    CheckedTextView temp;

    protected void sendComment(String comment) {
        ResponseHandler handler = new ResponseHandler(TAG_COMMENT);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        handler.setOnNeedLoginListener(this);
        PhotoMode.sendComment(getApplicationContext(), handler, article_uuid, comment, null, type);
    }

    protected void upComment(String uuid) {
        ResponseHandler handler = new ResponseHandler(TAG_UP);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        PhotoMode.upComment(getApplicationContext(), handler, uuid, "Comment");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==RequestCodeUtil.getInstance().obtainRequestCode(SendCommentActivity.class)){
            if (resultCode==RESULT_OK){
                refreshDate();
            }
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendComment(v.getText().toString());
        }
        return true;
    }
}
